<?php


namespace App\Models;


class Lesson extends Model
{
    protected $table = 'lessons';


    // ============= scope ==========
    public function scopeOfCoursePublishedLesson($query, $courseId)
    {
        return $query->where('course_id', $courseId)->where('is_published', static::IS_TRUE)->where('sequence', '!=', 0);
    }

    // ============= relations ==========
    public function course()
    {
        return $this->belongsTo(Course::class);
    }

    public function audio()
    {
        return $this->belongsTo(Attachment::class, 'name_audio');
    }

    public function cover()
    {
        return $this->belongsTo(Attachment::class, 'cover');
    }

    public function thumbCover()
    {
        return $this->belongsTo(Attachment::class, 'thumb_cover');
    }

    public function knowledge()
    {
        return $this->belongsToMany(Knowledge::class, 'knowledge_lesson');
    }

    public function assistWord()
    {
        return $this->belongsToMany(AssistWord::class, 'assist_word_lesson');
    }

    // ============= field ==========
    public function getCourseId()
    {
        return $this->course_id;
    }
    public function setCourseId($courseId)
    {
        $this->course_id = $courseId;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setAudio($audio)
    {
        $this->name_audio = $audio;
    }

    public function setCover($cover)
    {
        $this->cover = $cover;
    }

    public function getCover()
    {
        return $this->cover;
    }

    public function setThumbCover($thumbCover)
    {
        $this->thumb_cover = $thumbCover;
    }

    public function setLexile($lexile)
    {
        $this->lexile = $lexile;
    }

    public function getLexile()
    {
        return $this->lexile;
    }

    public function setIntroduction($introduction)
    {
        $this->introduction = json_encode($introduction);
    }

    public function getIntroduction()
    {
        return json_decode($this->introduction, true);
    }

    public function getSequence()
    {
        return $this->sequence;
    }
    public function setSequence($sequence)
    {
        $this->sequence = $sequence;
    }

    public function setWordCount($wordCount)
    {
        $this->word_count = $wordCount;
    }

    public function getWordCount()
    {
        return $this->word_count;
    }

    public function getKnowledgeCount()
    {
        return $this->knowledge_count;
    }

    public function setKnowledgeCount()
    {
        $this->knowledge_count = $this->knowledge()->count();
    }

    public function getGameTestCount()
    {
        return $this->game_test_count;
    }

    public function setGameTestCount($count)
    {
        $this->game_test_count = $count;
    }

    public function setGameWordCount($count)
    {
        $this->game_word_count = $count;
    }

    public function getGameWordCount()
    {
        return $this->game_word_count;
    }

    public function getEssenceQaCount()
    {
        return $this->essence_qa_count;
    }
    public function setEssenceQaCount($count)
    {
        $this->essence_qa_count = $count;
    }

    public function getPublished()
    {
        return $this->is_published;
    }

    public function setPublished($status)
    {
        $this->is_published = $status;
    }

    public function getStudiedCount()
    {
        return $this->studied_count;
    }


    public static function getNextSequence($courseId)
    {
        $sequence = static::where('course_id', $courseId)->orderBy('sequence', 'desc')->value('sequence');
        return $sequence ? $sequence + 1 : 0;
    }
}
